package com.itany.controller;

import com.itany.exception.UserErrorException;
import com.itany.exception.UserNotExistException;
import com.itany.pojo.ManagerUser;
import com.itany.service.ManagerUserService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/managerUser")
public class ManagerUserController {
    @Autowired
    ManagerUserService managerUserService;

    @RequestMapping("/login")
    public ActionResult login(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session){
        ActionResult result = new ActionResult();
        try {
            ManagerUser managerUser = managerUserService.login(username,password);
            session.setAttribute("managerUser",managerUser);
            result.setStatus(1);
            result.setMsg("成功");
        } catch (UserNotExistException e) {
            result.setStatus(0);
            result.setMsg(e.getMessage());
        } catch (UserErrorException e) {
            result.setStatus(0);
            result.setMsg(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            result.setStatus(0);
            result.setMsg("服务器内部异常");
        }
        return result;
    }
}
